<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Vue.js</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel="stylesheet" href="style.css" />
    <script src="vue.js"></script>
  </head>
  <body onload="draw()">
    <div id="vue-app"></div>
    <canvas id="canvas" width="800" height="600">
      你这浏览器不支持这玩意啊
    </canvas>
    <script src="app.js"></script>
    <script>
      function draw() {
        // 让vscode可以显示canvas智能提示
        /** @type {HTMLCanvasElement} */
        const canvas = document.getElementById('canvas')
        if (canvas.getContext) {
          const ctx = canvas.getContext('2d')

          // 用promise对象判断是否加载完毕
          const loadImage = function (src) {
            
            return new Promise((resolve, reject) => {
              const img = new Image()
              img.src = src
              aim = img
              aim.onload = () => {
                resolve(aim)
              }
              aim.onerror = (e) => {
                reject(e)
              }
            })
          }

          const loadResult = loadImage('hello.png')
          loadResult
            .then((res) => {
              const pattern = ctx.createPattern(res,"no-repeat")
              ctx.fillStyle = pattern
              ctx.fillRect(0, 0, 1000, 1000)
            })
            .catch((e) => {
              console.log('error', e)
            })
        }
      }
    </script>
  </body>
</html>
